using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using GoogleEarthClasses.Global;
using GoogleEarthClasses.Data;
using GoogleEarthClasses.Entities;
using System.IO;
using System.Text.RegularExpressions;
namespace GoogleEarthClasses.Logic
{
    public class LImage
    {
        #region QUERY
        public static DataTable SelectPhotosByPosition(Int32 IdPosition)
        {
            return DImage.SelectPhotosByPosition(IdPosition);
        }
        #endregion

        public static void DeleteIncompleted(int PositionId)
        {
            DImage.DeleteIncompleted(PositionId);
        
        }

        public static List<EImage> FindImagesByPosition(string PositionId, string ImagesFolder)
        {
            List<EImage> _ListImages = new List<EImage>();
            DirectoryInfo _Dir = null;
            FileInfo[] _Files = null;
            EImage _ImageTemp;
            try
            {
                //TODO: CHANGE THE IDs OF THE IMAGES!!!! --> NEW IMAGES ID = AssetId_ImageId.jpeg
                _Dir = new DirectoryInfo(ImagesFolder);
                _Files = _Dir.GetFiles("*.jpeg");
                Regex _ValidarFileForPoint = new Regex("^" + PositionId + "_[0-9_]+.jpeg$");
                foreach (FileInfo _File in _Files)
                {
                    if (_ValidarFileForPoint.Match(_File.Name).Success)
                    {
                        _ImageTemp = new EImage();
                        _ImageTemp.ContentPath = _File.FullName;
                        _ImageTemp.ImageBytes = GetImageBytes(_File.FullName);
                        _ImageTemp.Name = _File.Name;
                        _ImageTemp.ImageId = GetImageId(_File.Name);
                        
                        _ListImages.Add(_ImageTemp);
                    }
                }
                return _ListImages;
            }
            catch (Exception)
            {
                throw;
            }
        }
        private static int GetImageId(string FileName)
        {
            return int.Parse(FileName.Substring(FileName.IndexOf("_")+1, 1));
        }
        public static byte[] GetImageBytes(string FileName)
        {
            FileStream _FileStream = null;
            BinaryReader _BinaryReader = null;
            try
            {
                _FileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                _BinaryReader = new BinaryReader(_FileStream);
                byte[] ImageBytes = _BinaryReader.ReadBytes((int)_FileStream.Length);
                return ImageBytes;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                _BinaryReader.Close();
                _FileStream.Close();
                _FileStream.Dispose();

            }
        }

        public static byte[] GetImage(int ImageId)
        {
            return DImage.GetImage(ImageId);
            
        }

    }
}
